# Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
# Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this list of
#    conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice, this list
#    of conditions and the following disclaimer in the documentation and/or other materials
#    provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its contributors may be used
#    to endorse or promote products derived from this software without specific prior written
#    permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

ROOT_DIR = $(shell pwd)
SRC_DIR = src
OBJ_DIR = out
BIN_DIR = build
DEMO_DIR = demo

SHARED_LIB_SOURCES = $(wildcard $(SRC_DIR)/*.c)
SHARED_LIB_OBJECTS = $(SHARED_LIB_SOURCES:$(SRC_DIR)/%.c=$(OBJ_DIR)/%.o)

DEMO_SOURCES = $(wildcard $(DEMO_DIR)/*.c)
DEMO_OBJECTS = $(SHARED_LIB_SOURCES:$(SRC_DIR)/%.c=$(OBJ_DIR)/%.o)

# for linux secure compile options
SECURE_CFLAG_FOR_SHARE_LIBRARY = -D_FORTIFY_SOURCE=2 -fstack-protector-strong -fno-common -fPIC
SECURE_LDFLAG_FOR_SHARE_LIBRARY = -fPIC -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now -Wl,--gc-sections -Wl,--no-undefined -Wl,--as-needed

SECURE_CFLAG_FOR_DEMO= -D_FORTIFY_SOURCE=2 -fstack-protector-strong -fno-common -fPIC
SECURE_LDFLAG_FOR_DEMO = -fPIC -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now -Wl,--gc-sections -Wl,--no-undefined -Wl,--as-needed

SHARED_LIB_CFLAGS = $(SECURE_CFLAG_FOR_SHARE_LIBRARY)
SHARED_LIB_LDFLAGS = $(SECURE_LDFLAG_FOR_SHARE_LIBRARY)
DEMO_CFLAGS = $(SECURE_CFLAG_FOR_DEMO)
DEMO_LDFLAGS = $(SECURE_LDFLAG_FOR_DEMO)

# shared library link
# LIBS += -lbluetooth-internal -lshared-mainloop
SHARED_LIB_LIBS += -lbluetooth
DEMO_LIB_LIBS += -lbluetooth

# thirdparty include and link
SHARED_LIB_LDFLAGS += -L$(ROOT_DIR)/lib_install/aarch64/lib -L$(ROOT_DIR)/out/aarch64/bluez-5.63/lib/.libs/ -L$(ROOT_DIR)/out/aarch64/bluez-5.63/src/.libs/
SHARED_LIB_INCLUDES += -I$(ROOT_DIR)/lib_install/aarch64/include
SHARED_LIB_CFLAGS += -O2 -Wall -Wextra -Werror=format-security
DEMO_LDFLAGS += -L$(ROOT_DIR)/lib_install/aarch64/lib -L$(ROOT_DIR)/out/aarch64/bluez-5.63/lib/.libs/ -L$(ROOT_DIR)/out/aarch64/bluez-5.63/src/.libs/
DEMO_INCLUDES += -I$(ROOT_DIR)/lib_install/aarch64/include
DEMO_CFLAGS += -O2 -Wall -Wextra -Werror=format-security


# for debug
DEBUG_SHARED_LIB_CFLAGS = -g3 -DDEBUG -fsanitize=address -fsanitize=undefined
DEBUG_SHARED_LIB_LDFLAGS = -fsanitize=address -fsanitize=undefined

# for release
RELEASE_CFLAGS = -DNDEBUG -flto
RELEASE_LDFLAGS = -flto

# default compile target and default arch
TARGET_ARCH=aarch64-linux-gnu
SHARED_LIB = $(BIN_DIR)/sharekit_driver.so
DEMO = $(BIN_DIR)/ShareKit_TX_Demo
UNSTRIP_SHARED_LIB = $(BIN_DIR)/sharekit_driver.unstripped.so

# define compile commmand
export CC=/usr/bin/${TARGET_ARCH}-gcc
export CXX=/usr/bin/${TARGET_ARCH}-g++
export AR=/usr/bin/${TARGET_ARCH}-ar
export RANLIB=/usr/bin/${TARGET_ARCH}-ranlib
export LD=/usr/bin/${TARGET_ARCH}-ld
export STRIP=/usr/bin/${TARGET_ARCH}-strip

# shared library compile and link
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c | $(OBJ_DIR)
	$(CC) $(SHARED_LIB_CFLAGS) $(INCLUDES) -c $< -o $@

$(SHARED_LIB):$(SHARED_LIB_OBJECTS)
	@echo "==========start link========="
	$(CC) -shared $(SHARED_LIB_LDFLAGS) -o $(SHARED_LIB) $(SHARED_LIB_OBJECTS) $(LIBS)
	cp $(SHARED_LIB)  $(UNSTRIP_SHARED_LIB)
	$(STRIP) --strip-all $(UNSTRIP_SHARED_LIB)
	@echo "==========link finished========="

sharekit_driver:$(SHARED_LIB)

# demo compile and link
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c | $(OBJ_DIR)
	$(CC) $(SHARED_LIB_CFLAGS) $(INCLUDES) -c $< -o $@

$(SHARED_LIB):$(SHARED_LIB_OBJECTS)
	@echo "==========start link========="
	$(CC) -shared $(SHARED_LIB_LDFLAGS) -o $(SHARED_LIB) $(SHARED_LIB_OBJECTS) $(LIBS)
	cp $(SHARED_LIB)  $(UNSTRIP_SHARED_LIB)
	$(STRIP) --strip-all $(UNSTRIP_SHARED_LIB)
	@echo "==========link finished========="

demo:$(DEMO)

# clean
clean:
	@echo "==========start cleaning========="
	rm -f $(OBJ_DIR)/*.o $(OBJ_DIR)/*.d
	@echo "==========clean finished========="

all: sharekit_driver demo

rebuild: clean all

.PHONY: all clean rebuild sharekit_driver
